package com.contractpage.ec.service.impl;


import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.contractpage.common.persistence.Page;
import com.contractpage.ec.dao.AppointDao;
import com.contractpage.ec.dao.ContractDetailDao;
import com.contractpage.ec.dao.ContractInboundDao;
import com.contractpage.ec.dao.ContractInboundHistroyDao;
import com.contractpage.ec.dao.ContractStatusHistroyDao;
import com.contractpage.ec.entity.Appoint;
import com.contractpage.ec.entity.ContractDetail;
import com.contractpage.ec.entity.ContractInbound;
import com.contractpage.ec.entity.ContractInboundHistroy;
import com.contractpage.ec.entity.ContractStatusHistroy;
import com.contractpage.ec.service.ContractInboundService;



/**
 * 
 * ContractInboundServiceImpl数据库操作接口类
 * 
 **/
@Transactional
@Service
public class ContractInboundServiceImpl implements ContractInboundService{
	@Autowired
	private ContractInboundDao contractInboundDao;
	@Autowired
	private ContractDetailDao contractDetailDao;
	@Autowired
	private ContractInboundHistroyDao contractInboundHistroyDao;
	@Autowired
	private ContractStatusHistroyDao contractStatusHistroyDao;
	@Autowired
	private AppointDao appointDao;
	
	
	@Override
	public Page<ContractInbound> findList(Page<ContractInbound> page,ContractInbound entity){
		entity.setPage(page);
		page.setList(contractInboundDao.findList(entity));
		return page;
	}
	
	@Override
	public int insert(ContractInbound entity){
		String[] prints = entity.getPrints();
		Integer[] numbers = entity.getNumbers();
		int i = 0;
		for(; i < prints.length ; i++ ){
			entity.preInsert("");
			entity.setTotal(numbers[i]);
			entity.setPrinter(prints[i]);
			contractInboundDao.insert(entity);
			buildpublic(entity);
		}
		return i;
	}
	@Override
	public int update(ContractInbound entity){
		entity.preUpdate();
		buildpublic(entity);
		if(entity.getTotal() != null){
			Integer total = entity.getTotal();
			entity = contractInboundDao.get(entity);
			entity.setTotal(total + entity.getTotal());
		}
		int i = contractInboundDao.update(entity);
		return i;
	}
	
	private ContractStatusHistroy buildStatusHistroy(ContractDetail detail){
		ContractStatusHistroy histroy = new ContractStatusHistroy();
		histroy.setDetailid(detail.getId());
		Integer sequence = contractStatusHistroyDao.getMaxSequence(histroy);
		histroy.setSequence(sequence==null?1:sequence + 1);
		histroy.preInsert("");
		histroy.setConfirtrackstatus(detail.getConfirtrackstatus()==null?"0":detail.getConfirtrackstatus());
		histroy.setRemark(detail.getRemarks());
		histroy.setStatus(detail.getStatus());
		histroy.setCreateBy(detail.getCreateBy());
		histroy.setUpdateDate(new Date());
		return histroy;
	}
	
	private void buildpublic(ContractInbound entity){
		ContractDetail contractDetail = new ContractDetail();
		if(entity.getTotal() != null){
			ContractInboundHistroy history = new ContractInboundHistroy();
			history.preInsert("");
			history.setInboundid(entity.getId());
			history.setRemarks(entity.getRemark());
			history.setAmount(entity.getTotal());
			history.setPrinter(entity.getPrinter());
			Integer batch = contractInboundHistroyDao.getBatch(history);
			history.setBatch(batch == null?1:contractInboundHistroyDao.getBatch(history) + 1);
			for(int j = 0 ; j < entity.getTotal() ; j++){
				contractDetail.setProjectcode(entity.getProjectcode());
				contractDetail.setProjectname(entity.getProjectname());
				contractDetail.setInboundid(entity.getId());
				contractDetail.setType(contractDetail.getType());
				contractDetail.setStatus("0");
				contractDetail.preInsert("");
				contractDetail.setBatch(history.getBatch());
				contractStatusHistroyDao.insert(buildStatusHistroy(contractDetail));
				contractDetailDao.insert(contractDetail);
			}
			contractInboundHistroyDao.insert(history);
		}
	}
	@Override
	public int delete(ContractInbound entity){
		 return contractInboundDao.delete(entity);
	}

	@Override
	public Page<ContractInbound> findDistributes(Page<ContractInbound> page,ContractInbound entity) {
		entity.setPage(page);
		page.setList(contractInboundDao.findDistributes(entity));
		return page;
	}

	@Override
	public Integer saveDistributes(ContractInbound entity) {
		//entity.setPrinter("1");
		ContractInbound inbound = contractInboundDao.get(entity);
		ContractDetail detail = new ContractDetail();
		detail.setStatus("2");
		detail.setInboundid(inbound.getId());
		//detail.setInboundid(entity.getId());
		List<ContractDetail> details = contractDetailDao.findList(detail);
		List<Appoint> apponts = entity.getApps();
		int l = apponts.size();
		for(int i = 0 ; i < l ; i ++){
			Appoint app = new Appoint();
			app.setId(apponts.get(i).getId());
			//app = appointDao.get(app);
			app.setStatus("1");
			appointDao.update(app);
			detail = details.get(i);
			detail.setReserveNo(apponts.get(i).getReserveNo());
			detail.setStatus("3");
			contractDetailDao.update(detail);
		}
		return l;
	}

	@Override
	public Page<ContractInbound> findContractAppoint(Page<ContractInbound> page,ContractInbound entity) {
		entity.setPage(page);
		page.setList(contractInboundDao.findContractAppoint(entity));
		return page;
	}

	@Override
	public ContractInbound get(ContractInbound entity) {
		return contractInboundDao.get(entity);
	}

	@Override
	public List<ContractInbound> findQs(ContractInbound entity) {
		return contractInboundDao.findList(entity);
	}
}